package com.qilu.pe.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Config;
import com.qilu.pe.base.Global;
import com.qilu.pe.base.MyApp;
import com.qilu.pe.dao.bean.Doctor;
import com.qilu.pe.dao.bean.EventModel;
import com.qilu.pe.dao.bean.ExpertUser;
import com.qilu.pe.dao.bean.MineMenu;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.GlideUtil;
import com.qilu.pe.support.util.PeUtil;
import com.qilu.pe.ui.activity.EditMyInfoActivity;
import com.qilu.pe.ui.activity.MyBalanceActivity;
import com.qilu.pe.ui.activity.MyClinicActivity;
import com.qilu.pe.ui.activity.MyCollectActivity;
import com.qilu.pe.ui.activity.MyIncomeActivity;
import com.qilu.pe.ui.activity.MyOrderActivity;
import com.qilu.pe.ui.activity.MyProductActivity;
import com.qilu.pe.ui.activity.MyScoreActivity;
import com.qilu.pe.ui.activity.SettingActivity;
import com.ruitu.arad.Arad;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.PhoneUtils;
import com.ruitu.arad.util.ScreenUtils;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class Home5Fragment extends BaseFragment {
    private RecyclerView rcv_mine_menu;
    private ImageView iv_setting;// 设置
    private RoundedImageView iv_header, iv_special_header;// 头像 专属顾问头像
    private RelativeLayout rl_top;// 顶部的用户信息
    private TextView tv_name, tv_user_tag, tv_year, tv_phone, tv_special_name;

    private MineMenuAdapter adapter;
    private List<MineMenu> menuList = new ArrayList<>();
    private static final String[] NAME_ARR = {"我的订单", "我的诊所", "我的收入",
            "我的收藏", "我的商品", "我的积分", "我的余额"};
    private static int[] ICON_ID = {R.mipmap.ic_mine_menu_0, R.mipmap.ic_mine_menu_1,
            R.mipmap.ic_mine_menu_2, R.mipmap.ic_mine_menu_4, R.mipmap.ic_mine_menu_5,
            R.mipmap.ic_mine_menu_6, R.mipmap.ic_mine_menu_7};
    private static final String[] NAME_ARR1 = {"我的订单", "我的诊所", "我的收入",
            "我的收藏",  "我的积分", "我的余额"};
    private static int[] ICON_ID1 = {R.mipmap.ic_mine_menu_0, R.mipmap.ic_mine_menu_1,
            R.mipmap.ic_mine_menu_2, R.mipmap.ic_mine_menu_4,
            R.mipmap.ic_mine_menu_6, R.mipmap.ic_mine_menu_7};
    private int TO_PAGE = 1;// 获取用户信息后,标识跳转的页面 1 我的机构
    private Doctor doctor;// 请求的用户信息
    private  int mallSwitch=1;

    public Home5Fragment() {

    }

    public static Home5Fragment newInstance() {
        Home5Fragment fragment = new Home5Fragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new MineMenuAdapter(getActivity());
        String mall_switch=Arad.preferences.getString(Config.MALL_SWITCH);
        if(!TextUtils.isEmpty(mall_switch)){
            mallSwitch=Integer.valueOf(mall_switch);
            if(mallSwitch == 0){
                for (int i = 0; i < 6; i++) {
                    menuList.add(new MineMenu(NAME_ARR1[i], ICON_ID1[i]));
                }
            }else if(mallSwitch == 1){
                for (int i = 0; i < 7; i++) {
                    menuList.add(new MineMenu(NAME_ARR[i], ICON_ID[i]));
                }
            }
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home5, container, false);
        rcv_mine_menu = v.findViewById(R.id.rcv_mine_menu);
        iv_setting = v.findViewById(R.id.iv_setting);
        rl_top = v.findViewById(R.id.rl_top);
        iv_header = v.findViewById(R.id.iv_header);
        tv_name = v.findViewById(R.id.tv_name);
        tv_user_tag = v.findViewById(R.id.tv_user_tag);
        tv_year = v.findViewById(R.id.tv_year);
        tv_phone = v.findViewById(R.id.tv_phone);
        tv_special_name = v.findViewById(R.id.tv_special_name);
        iv_special_header = v.findViewById(R.id.iv_special_header);

        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        rcv_mine_menu.setLayoutManager(manager);
        rcv_mine_menu.setAdapter(adapter);
        adapter.setDataList(menuList);

        setlisteners();

        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        reqUserInfo();
    }

    // 获取用户信息
    private Disposable reqUserInfo() {
        return APIRetrofit.getDefault().getUserInfo(Global.HEADER, "1")
                .compose(RxUtil.<BaseResult2<List<Doctor>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Doctor>>>() {
                    @Override
                    public void accept(BaseResult2<List<Doctor>> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {
                            List<Doctor> doctorList = result2.getData();
                            if (!isDataEmpty(doctorList)) {
                                doctor = doctorList.get(0);
                                tv_name.setText(doctor.getName());
                                tv_user_tag.setText(TextUtils.equals("1", doctor.getIs_admin()) ? "负责人" : "医生");
                                tv_year.setText("从业年限：" + doctor.getYear() + "年");
                                tv_phone.setText(doctor.getPhone());

                                tv_special_name.setText(doctor.getCounselor());
                                GlideUtil.loadImg(PeUtil.getImageUrl(doctor.getPicture()), iv_header);
//                                GlideUtil.loadImg(PeUtil.getImageUrl(doctor.getPicture()), iv_special_header);

                                Global.ORG_BALANCE = doctor.getMoney();
                                Global.ORG_ADDRESS = doctor.getOrg_address();
                                Arad.bus.post(new EventModel(20101452));
                            }
                        } else {
                            tv_name.setText("未登录");
                            tv_user_tag.setText("");
                            tv_year.setText("从业年限：" + "0年");
                            tv_phone.setText("");
                        }
//                        Logg.i("reqUserInfo.result2.getMsg()" + result2.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        tv_name.setText("未登录");
                        tv_user_tag.setText("");
                        tv_year.setText("从业年限：" + "0年");
                        tv_phone.setText("");
                    }
                });
    }

    // 专家端获取用户信息
    private Disposable reqUserInfo2() {
        return APIRetrofit.getDefault().reqUserInfo(Global.HEADER)
                .compose(RxUtil.<BaseResult2<ExpertUser>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<ExpertUser>>() {
                    @Override
                    public void accept(BaseResult2<ExpertUser> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {

                        } else {

                        }
                        ExpertUser u = result2.getData();
                        Global.ORG_ID = u.getOrg_id();

                        if (TO_PAGE == 1) {
                            startActivity(MyClinicActivity.class);
                        } else if (TO_PAGE == 2) {

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private void setlisteners() {
        setOnClickListener(iv_setting, rl_top, iv_header, tv_special_name, iv_header);

        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                if(mallSwitch == 0){
                    if (position == 0) {// 我的订单
                        startActivity(MyOrderActivity.class);
                    } else if (position == 1) {// 我的诊所
                        if (TextUtils.equals("1", doctor.getIs_admin())) {
                            TO_PAGE = 1;
                            hideProgress();
                            reqUserInfo2();
                            showProgress();
                        } else {
                            ToastUtils.showShort("抱歉!您没有查看权限");
                        }
                    } else if (position == 2) {// 我的收入
                        startActivity(MyIncomeActivity.class);
                    } else if (position == 3) {// 我的收藏
                        startActivity(MyCollectActivity.class);
                    }
//                    else if (position == 4) {// 我的商品
//                        startActivity(MyProductActivity.class);
//                    }
                    else if (position == 4) {// 我的积分
                        startActivity(MyScoreActivity.class);
                    } else if (position == 5) {// 我的余额
                        if (TextUtils.equals("1", doctor.getIs_admin())) {
                            startActivity(MyBalanceActivity.class);
                        } else {
                            ToastUtils.showShort("抱歉!您没有查看权限");
                        }
                    } else {
                        ToastUtils.showShort("欢迎使用齐鲁云检测");
                    }
                }else if(mallSwitch == 1){
                    if (position == 0) {// 我的订单
                        startActivity(MyOrderActivity.class);
                    } else if (position == 1) {// 我的诊所
                        if (TextUtils.equals("1", doctor.getIs_admin())) {
                            TO_PAGE = 1;
                            hideProgress();
                            reqUserInfo2();
                            showProgress();
                        } else {
                            ToastUtils.showShort("抱歉!您没有查看权限");
                        }
                    } else if (position == 2) {// 我的收入
                        startActivity(MyIncomeActivity.class);
                    } else if (position == 3) {// 我的收藏
                        startActivity(MyCollectActivity.class);
                    } else if (position == 4) {// 我的商品
                        startActivity(MyProductActivity.class);
                    } else if (position == 5) {// 我的积分
                        startActivity(MyScoreActivity.class);
                    } else if (position == 6) {// 我的余额
                        if (TextUtils.equals("1", doctor.getIs_admin())) {
                            startActivity(MyBalanceActivity.class);
                        } else {
                            ToastUtils.showShort("抱歉!您没有查看权限");
                        }
                    } else {
                        ToastUtils.showShort("欢迎使用齐鲁云检测");
                    }
                }

            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v == iv_setting) {// 设置
            startActivity(SettingActivity.class);
        }
        if (v == rl_top) {// 用户信息
//            startActivity(ModifyUserInfoActivity.class);
        }
        if (v == iv_header) {// 头像
            reqUserInfo();
        }
        if (v == tv_special_name) {// 专属顾问拨打电话
//            PhoneUtils.dial("10086");
            if (null != doctor && !TextUtils.isEmpty(doctor.getTel())) {
                PhoneUtils.dial(doctor.getTel());
            } else {
                ToastUtils.showShort("暂无专属顾问");
            }
        }
        if (v == iv_header) {
            startActivity(new Intent(getContext(), EditMyInfoActivity.class)
                    .putExtra("id", doctor.getId()));
        }
    }

    private class MineMenuAdapter extends ListBaseAdapter<MineMenu> {

        public MineMenuAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_mine_menu_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RelativeLayout rl_item = holder.getView(R.id.rl_item);
            ImageView iv_icon = holder.getView(R.id.iv_icon);
            TextView tv_name = holder.getView(R.id.tv_name);

            ViewGroup.LayoutParams params = rl_item.getLayoutParams();
            params.height = ScreenUtils.getScreenWidth() / 3;
            rl_item.setLayoutParams(params);

            MineMenu menu = mDataList.get(position);
            tv_name.setText(menu.getName());
            iv_icon.setImageResource(menu.getResId());
        }
    }
}